package com.leetcode.offer.chapter4;

import com.leetcode.basic.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dennis Li
 * @date 2020/7/14 9:59
 */
public class PathSumCollector_34 {

    List<List<Integer>> ans;
    List<Integer> aux;

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        ans = new ArrayList<>();
        aux = new ArrayList<>();
        dfs(root, sum);
        return ans;
    }

    private void dfs(TreeNode root, int sum) {
        if (root == null) return;
        aux.add(root.val);
        if (sum == root.val && root.left == null && root.right == null) {
            ans.add(new ArrayList<>(aux));
        } else {
            dfs(root.left, sum - root.val);
            dfs(root.right, sum - root.val);
        }
        aux.remove(aux.size() - 1);
    }
}
